package se.webbzon.boltzmann.editor.component.texturelist;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.SortedSet;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.component.AbstractComponent;
import se.webbzon.boltzmann.editor.list.NullableListModel;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;

public class TextureSetList extends AbstractComponent {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = -8468253516082266027L;

	// A list of all available textures in this panel
	private final JList<TextureSetEntry> textureList;
	
	// The input fields for this panel
	private final JTextField indexInput, speedInput;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public TextureSetList( SortedSet<TextureSetEntry> textures,
									String textureSetName,
									double textureIndex,
									double textureSpeed) {
		super("Texture");
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		JPanel texturePanel = new JPanel();
		texturePanel.setBorder(BorderFactory.createTitledBorder("Texture"));
		NullableListModel<TextureSetEntry> textureListModel =
				new NullableListModel<TextureSetEntry>();
		textureList = new JList<TextureSetEntry>(textureListModel);
		//textureList.setPreferredSize(new Dimension(480,480));
		textureList.setCellRenderer(new TextureSetListCell(new Dimension(480,64)));
		JScrollPane scrollPane = new JScrollPane(textureList);
		scrollPane.setPreferredSize(new Dimension(480,480));
		texturePanel.add(scrollPane);
		
		for (TextureSetEntry texture : textures) {
			textureListModel.addElement(texture);
			if (texture.getTextureSetName().equals(textureSetName))
				textureList.setSelectedIndex(textureListModel.getSize()-1);
		}
		
		JPanel settingsPanel = new JPanel();
		settingsPanel.setBorder(BorderFactory.createTitledBorder("Texture Settings"));
		settingsPanel.setLayout(new GridLayout(2,2));
		settingsPanel.add(new JLabel("Texture index:"));
		settingsPanel.add(new JLabel("Texture speed:"));
		indexInput = new JTextField("" + textureIndex);
		speedInput = new JTextField("" + textureSpeed);
		settingsPanel.add(indexInput);
		settingsPanel.add(speedInput);
		
		add(texturePanel);
		add(settingsPanel);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the selected texture set name from this panel. **/
	public String getTextureSetName() {
		TextureSetEntry entry = textureList.getSelectedValue();
		if (entry == null)
			return "";
		else
			return entry.getTextureSetName();
	}
	
	/** Returns the selected texture index from this panel. **/
	public double getTextureIndex() {
		return Double.parseDouble(indexInput.getText());
	}
	
	/** Returns the selected texture speed from this panel. **/
	public double getTextureSpeed() {
		return Double.parseDouble(speedInput.getText());
	}
	
	/** Returns the selected texture from this panel. **/
	public ResourceSet<Texture3D> getTextureSet() {
		TextureSetEntry entry = textureList.getSelectedValue();
		if (entry == null)
			return null;
		else
			return entry.getTextureSet();
	}

}
